package skean.yzsm.com.androidvideocache;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.blankj.utilcode.util.FileUtils;
import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.headers.EmptyHeadersInjector;
import com.danikula.videocache.headers.HeaderInjector;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class App extends Application {

    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy(Context context) {
        App app = (App) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        HttpProxyCacheServer.Builder builder = new HttpProxyCacheServer.Builder(this);
        File cacheFile = new File(Environment.getExternalStorageDirectory(), "CacheTest");
        FileUtils.createOrExistsDir(cacheFile);
        builder.cacheDirectory(cacheFile);
        HeaderInjector headerInjector = new HeaderInjector() {
            @Override
            public Map<String, String> addHeaders(String url) {
                HashMap<String, String> map = new HashMap<>();
                map.put("Authorization", "Basic c2tlYW46MTIzNDU2");
                return map;
            }
        };
        builder.headerInjector(headerInjector);
        return builder.build();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}
